<template>
    <div class="social-page">
        <!-- 顶部导航栏 -->
        <HeaderNav activeMenu="social" />

        <!-- 主内容区域 -->
        <div class="main-content">
            <!-- 左侧边栏 -->
            <Sidebar :active-sidebar="activeSidebar" @sidebar-select="handleSidebarSelect" />

            <!-- 右侧内容区域 -->
            <main class="social-main">
                <div class="social-content">
                    <!-- 搜索栏和我的动态按钮 -->
                    <div class="social-header">
                        <div class="search-section">
                            <el-input v-model="searchKeyword" placeholder="搜索动态或用户..." class="search-input"
                                @keyup.enter="handleSearch" clearable @clear="handleClearSearch">
                                <template #prepend>
                                    <el-select v-model="searchType" style="width: 110px" class="choose-text">
                                        <el-option label="动态内容" value="post" />
                                        <el-option label="用户昵称" value="user" />
                                    </el-select>
                                </template>
                                <template #append>
                                    <el-button @click="handleSearch" class="search-btn">
                                        <el-icon>
                                            <Search />
                                        </el-icon>
                                    </el-button>
                                </template>
                            </el-input>
                        </div>
                        <div class="action-buttons">
                            <el-button type="primary" @click="showMyPosts" class="my-posts-btn">
                                <el-icon>
                                    <User />
                                </el-icon>
                                我的动态
                            </el-button>
                        </div>
                    </div>

                    <PostList :search-keyword="currentSearchKeyword" :search-type="currentSearchType"
                        @refresh="handleRefreshPosts" />
                </div>
            </main>
        </div>

        <!-- 我的动态模态框 -->
        <el-dialog v-model="myPostsDialogVisible" title="我的动态" width="800px" :before-close="handleCloseMyPosts"
            class="my-posts-dialog">
            <MyPostsPanel v-if="myPostsDialogVisible" @close="myPostsDialogVisible = false"
                @post-updated="handlePostUpdated" @post-deleted="handlePostDeleted" />
        </el-dialog>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, User } from '@element-plus/icons-vue'
import HeaderNav from '@/components/layout/HeaderNav.vue'
import Sidebar from '@/components/layout/Sidebar.vue'
import PostList from '@/components/posts/PostList.vue'
import MyPostsPanel from '@/components/posts/MyPostsPanel.vue'

const router = useRouter()

// 响应式数据
const activeSidebar = ref('social')
const searchKeyword = ref('')
const searchType = ref('post')
const currentSearchKeyword = ref('')
const currentSearchType = ref('')
const myPostsDialogVisible = ref(false)

const handleSidebarSelect = (index) => {
    activeSidebar.value = index
    // 侧边栏导航逻辑
    switch (index) {
        case 'dashboard':
            router.push('/')
            break
        case 'chat':
            router.push('/chat')
            break
        case 'lost-found':
            router.push('/lost-and-found')
            break
        case 'second-hand':
            router.push('/second-hand')
            break
        case 'circles':
            router.push('/circle')
            break
        case 'network':
            router.push('/network')
            break
        case 'recruitment':
            router.push('/recruitment')
            break
        default:
            ElMessage.success(`当前已是在 ${index}页面`)
    }
}

// 搜索处理
const handleSearch = () => {
    if (!searchKeyword.value.trim()) {
        ElMessage.warning('请输入搜索关键词')
        return
    }

    currentSearchKeyword.value = searchKeyword.value.trim()
    currentSearchType.value = searchType.value
    ElMessage.success(`搜索: ${searchKeyword.value} (${searchType.value === 'post' ? '动态内容' : '用户昵称'})`)
}

// 清除搜索
const handleClearSearch = () => {
    currentSearchKeyword.value = ''
    currentSearchType.value = ''
    searchKeyword.value = ''
}

// 显示我的动态
const showMyPosts = () => {
    myPostsDialogVisible.value = true
}

// 关闭我的动态模态框
const handleCloseMyPosts = () => {
    myPostsDialogVisible.value = false
}

// 处理帖子更新
const handlePostUpdated = () => {
    ElMessage.success('动态更新成功')
    // 可以触发PostList刷新
}

// 处理帖子删除
const handlePostDeleted = () => {
    ElMessage.success('动态删除成功')
    // 可以触发PostList刷新
}

// 刷新帖子列表
const handleRefreshPosts = () => {

}
</script>


<style scoped>
.social-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4efe9 100%);
    display: flex;
    flex-direction: column;
}

/* 主内容区域 */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.social-main {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    margin-left: 0;
    background: transparent;
}

.social-content {
    max-width: 900px;
    margin: 0 auto;
    margin-left: 400px;
}

/* 搜索栏样式 */
.social-header {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.8);
}

.search-section {
    flex: 1;
}

.search-input {
    width: 100%;
}

:deep(.search-input .el-input-group__prepend) {

    border: none;
    color: white;
}

:deep(.search-input .el-input-group__prepend .el-select .el-input__inner) {
    color: white;
    background: transparent;
    border: none;
}

:deep(.search-input .el-input-group__prepend .el-select .el-input__suffix) {
    color: white;
}

.search-btn {
    border: none;
    color: white;
}

.search-btn:hover {
    background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.action-buttons {
    flex-shrink: 0;
}

.my-posts-btn {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    border: none;
    border-radius: 10px;
    padding: 10px 20px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.my-posts-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(245, 87, 108, 0.3);
}

/* 对话框样式 */
:deep(.my-posts-dialog .el-dialog) {
    border-radius: 16px;
    overflow: hidden;
}

:deep(.my-posts-dialog .el-dialog__header) {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    margin: 0;
    padding: 16px 20px;
}

:deep(.my-posts-dialog .el-dialog__title) {
    color: white;
    font-weight: 600;
}

:deep(.my-posts-dialog .el-dialog__headerbtn) {
    top: 16px;
}

:deep(.my-posts-dialog .el-dialog__headerbtn .el-dialog__close) {
    color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .social-header {
        flex-direction: column;
        gap: 12px;
        padding: 16px;
        border-radius: 12px;
    }

    .search-section {
        width: 100%;
    }

    .action-buttons {
        width: 100%;
    }

    .action-buttons .el-button {
        width: 100%;
    }

    .social-main {
        padding: 16px;
    }
}
</style>
